import datetime
from django.db import connection

def getdatestr():
    # 两天前 YYYY-MM-DD 默认格式
    yesterday = datetime.date.today() - datetime.timedelta(days=0)
    return str(yesterday)


def convert_to_dicts(objs):
    '''把对象列表转换为字典列表'''
    obj_arr = []

    for o in objs:
        # 把Object对象转换成Dict
        dict = {}
        dict.update(o.__dict__)
        dict.pop("_state", None)  # 去除掉多余的字段
        obj_arr.append(dict)

    return obj_arr


def getresult(sql):
    cursor = connection.cursor()
    cursor.execute(sql)
    SqlDomain = cursor.description
    keys=[]
    for i in range(len(SqlDomain)):
        keys.append(SqlDomain[i][0])
    print(keys)
    rowvalue = cursor.fetchone()
    jsonvalues=[]
    while rowvalue:
        keyid=0
        jsonvalue={}
        for val1 in rowvalue:
            jsonvalue[keys[keyid]]=str(val1)
            keyid = keyid + 1
        jsonvalues.append(jsonvalue)
        rowvalue = cursor.fetchone()
    print(jsonvalues)
    return jsonvalues
